package com.octant.testipc;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IDevListener;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.bean.DeviceBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {

    private Context mContext;
    private List<DeviceBean> mList;

    public DeviceAdapter(Context mContext, List<DeviceBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_device_item, parent, false);
        DeviceViewHolder holder = new DeviceViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceAdapter.DeviceViewHolder holder, int position) {
        DeviceBean deviceBean = mList.get(position);
        holder.tv_name.setText(deviceBean.getName());
        if (deviceBean.getIsOnline()){
            holder.tv_status.setText("在线");
        }else {
            holder.tv_status.setText("离线");
        }
        if (deviceBean.iconUrl != null){
            Glide.with(mContext).load(deviceBean.iconUrl).into(holder.iv_icon);
        }
        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null){
                    itemClickListener.onClick(position);
                }
            }
        });
        holder.btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null){
                    itemClickListener.onRemove(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class DeviceViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rl_item;
        private TextView tv_name,tv_status;
        private ImageView iv_icon;
        private Button btn_remove;

        public DeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            rl_item = itemView.findViewById(R.id.rl_item);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_status = itemView.findViewById(R.id.tv_status);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            btn_remove = itemView.findViewById(R.id.btn_remove);
        }
    }

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener{

        void onClick(int pos);
        void onRemove(int pos);

    }
}
